package com.webex.ta.hydra.gui;

import com.webex.ta.hydra.util.HydraUtils;

import javax.swing.*;
import java.awt.*;

/**
 * Author: Vegaz
 * Date: 2010-11-3
 * Time: 9:12:24
 */
public class SplashScreen extends JFrame {
    private final Image image;

    private static final int PROGRESS_HEIGHT = 20;
    private static int PROGRESS_COUNT = 100;
    private int progress;
    private String label;
    private Font defaultFont = new Font("Dialog", Font.PLAIN, 11);
    private Font labelFont = UIManager.getFont("Label.font").deriveFont(10f);
    private final FontMetrics fm;
    private Color versionColor1 = new Color(55, 55, 55);
    private Color versionColor2 = new Color(255, 255, 255, 50);

//    public SplashScreen() {
//        this(null);
//    }
    
    public SplashScreen() {
//        super(owner);
        this.setIconImage(HydraUtils.getImage("hydra.jpg").getImage());
        this.setTitle(HydraIdeFrame.DEFAULT_TITLE);
        this.setUndecorated(true);
        image = getToolkit().getImage(
                getClass().getResource("/com/webex/ta/hydra/images/hydra3.jpg"));
        MediaTracker tracker = new MediaTracker(this);
        tracker.addImage(image, 0);

        try {
            tracker.waitForAll();
        } catch (Exception ignore) {

        }
//        this.setAlwaysOnTop(true);
        fm = getFontMetrics(defaultFont);
        label = "Initializing";
    }

    public void setLabel(String label) {
        this.label = label;
        repaint();
    }

    @Override
    public Dimension getPreferredSize() {
        int imageWidth = image.getWidth(null);
        int imageHeight = image.getHeight(null);
        return new Dimension(imageWidth + 2, imageHeight + 2 + PROGRESS_HEIGHT);
    }

    @Override
    public void paint(Graphics g) {
        Dimension size = getSize();

        g.setColor(Color.black);
        g.drawRect(0, 0, size.width - 1, size.height - 1);
        g.drawImage(image, 1, 1, this);

        int imageHeight = image.getHeight(null);
        g.setColor(Color.white);
        g.fillRect(1, imageHeight + 1, getWidth() - 2, PROGRESS_HEIGHT);

        g.setColor(Color.gray);
        g.fillRect(1, imageHeight + PROGRESS_HEIGHT + 1,
                (getWidth() - 2) * progress / PROGRESS_COUNT, PROGRESS_HEIGHT);

        if (label != null) {
//            int drawOffsetX = (getWidth() - fm.stringWidth(label)) / 2;
            int drawOffsetX = 10;
            int drawOffsetY = imageHeight + (PROGRESS_HEIGHT
                    + fm.getAscent() + fm.getDescent()) / 2;

            paintString(g, label, drawOffsetX, drawOffsetY);
        }

        String version = "Version 0.0.1";
        int drawOffsetX = (getWidth() / 2) - (fm.stringWidth(version) / 2);
        int drawOffsetY = imageHeight - fm.getDescent() - 2 - 5;
        paintString(g, version, drawOffsetX, drawOffsetY);
    }


    private void paintString(Graphics g, String string, int drawOffsetX,
                             int drawOffsetY) {
        g.setFont(labelFont);

        g.setColor(versionColor1);
        g.drawString(string, drawOffsetX, drawOffsetY);
//         Draw a highlight effect
        g.setColor(versionColor2);
        g.drawString(string, drawOffsetX + 1, drawOffsetY + 1);
    }
}
